define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    var iconClassMap = require("core/utils/uploadUtil");
    $.widget('rk.groupContentFileCtrl', {
        _create:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._listeners();
        },
        _init:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me.loaddata("fileList",{gId:opt.gid,pId:-1});
        },
        _listeners:function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='group_file_path_button']", function(){
                var button = $(this);
                var pid = button.attr("pid");
                var pname = button.attr("pname");
                me.loaddata("fileList",{gId:opt.gid,pId:pid,pname:pname});
            });
            elem.on("click", "[act='group_file_downLoad_button']", function(){
                var button = $(this);
                var tfcode = button.attr("tfcode");
                var fileId = button.attr("fileId");
                window.location.href = "/json/sns_twitter_file/download.action?fileId="+fileId+"&checkCode="+tfcode;
            });
            elem.on("click", "[act='group_file_delete_button']", function(){
                var button = $(this);
                var fileId = button.attr("fileId");
                rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_DELETE'),{okText:rk.i18n('DELETE')}, function () {
                    rk.post("/json/sns_twitter_file/delete.action",{"fileId":fileId},function(json){
                        if(json.status === 0){
                            if(me.loaddataParam) {
                                elem.beLoading();
                                setTimeout(function(){
                                    me.loaddata(me.loaddataParam.type, me.loaddataParam.param);
                                }, 1000);
                            }
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }, function () {
                    //取消
                });
            });
            var groupQueryByName = function(){
                var key = elem.find("[act='group_file_query_name']").val();
                if(key){
                    me.loaddata("fileSearch", {gId:opt.gid,key:key});
                }else{
                    rk.noticeError(rk.i18n('GROUP_PLEASE_INPUT_CONDITION'));
                }
            }
            elem.on("keydown", "[act='group_file_query_name']", function(e){
                if(e.keyCode==13){
                    groupQueryByName();
                }
            });
            elem.on("click", "[act='group_file_query_name_button']", function(){
                groupQueryByName();
            });
            elem.on("click", "[act='group_file_favTag_button']", function(){
                var button = $(this);
                var favStatus = button.attr("favStatus");
                var fileId = button.attr("fileId");
                if(favStatus == 1){
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_COLLECT'), function () {
                        rk.post("/json/sns_twitter_file_fav/cancel.action",{"sid":fileId},function(json){
                            if(json.status === 0){
                                button.attr("favStatus","0");
                                button.removeClass("file_collect");
                                button.addClass("file_uncollect");
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            }else{
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                        });
                    }, function () {
                        //取消
                    });
                }else if(favStatus == 0){
                    if(button.hasClass('disable')) return;
                    button.addClass('disable');
                    rk.post("/json/sns_twitter_file_fav/save.action",{"sid":fileId},function(json){
                        if(json.status === 0){
                            button.removeClass("disable");
                            button.attr("favStatus","1");
                            button.removeClass("file_uncollect");
                            button.addClass("file_collect");
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        }else{
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }
            });
            elem.moveDoc({
                selector:'a.file_move',
                fidName:'fileId',
                success:function(){
                    if(me.loaddataParam) {
                        me.loaddata(me.loaddataParam.type, me.loaddataParam.param);
                    }
                },
                error:function(){

                }
            });
            elem.manageDir({
                selector: 'a.file_manage',
                success: function () {
                    if(me.loaddataParam) {
                        me.loaddata(me.loaddataParam.type, me.loaddataParam.param);
                    }
                },
                error: function () {
                    rk.noticeError(rk.i18n('TWITTERFILE_DIRECTORY_UPDATE_FAILURE'));
                }
            });
            var permission = [-1,2,3];
            if(opt.type == "depart"){
                permission = [-1,1,2,3]
            }else if(opt.type == "group" && opt.permission === 1){
                permission = [1,2,3]
            }
            //文件上传
            elem.fileUpload({
                permission:function() {
                    return permission;
                },
                selector: 'a.file_upload',
                url: function (){
                    return "/json/oa_twitter-file/save-group-files.action";
                },
                fullPath: function(){
                    var nav_path = '<span class="firsr">'+rk.i18n('GROUP_FILE_ALL')+'</span>';
                    if(me.pathCrumbs && me.pathCrumbs.length > 0){
                        $.each(me.pathCrumbs, function(i,dataJson){
                            nav_path += '<span class="sub-directory">' + dataJson.text + '</span>';
                        });
                    }
                    return nav_path || rk.i18n('GROUP_ROOT');
                },
                success: function () {
                    me.loaddata(me.loaddataParam.type, me.loaddataParam.param);
                },
                error: function () {
                    rk.noticeError(rk.i18n('TWITTERFILE_DIRECTORY_UPDATE_FAILURE'));
                }
            });
        },
        pathCrumbs:[],
        computePathCrumbs:function(pid,pname){
            var me = this;
            var pathCrumbs = me.pathCrumbs;
            if(pathCrumbs && pathCrumbs.length == 1){
                var ppid = pathCrumbs[0].pid;
                if(!ppid){
                    delete pathCrumbs[0];
                }
            }
            if(pid == "-1"){
                me.pathCrumbs = [];
                return;
            }
            if(pathCrumbs){
                var newPathCrumbs = [];
                for(var i=0;i<pathCrumbs.length;i++){
                    var dataJson = pathCrumbs[i];
                    if(dataJson.pid == pid){
                        break;
                    }
                    newPathCrumbs.push(dataJson);
                }
                newPathCrumbs.push({pid:pid,text:pname});
                me.pathCrumbs = newPathCrumbs;
            }
        },
        loaddataParam:"",
        _indexTableAuthMoved:null,
        loaddata:function(type,param){
            var temp = require("oa/tmpl/group/content/tpl_group_file_table.tpl");
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.beLoading();
            var showHtml = function(json){
                json.data.pathCrumbs = me.pathCrumbs;
                if(json.data.twitterFiles && json.data.twitterFiles.length > 0){
                    var newTwitterFiles = [];
                    $.each(json.data.twitterFiles, function(i,dataJson){
                        var updateDate = new Date(dataJson.updatedAt-0);
                        var year = updateDate.getFullYear();
                        var month = updateDate.getMonth()+1;
                        var day = updateDate.getDate();
                        month = (month+"").length == 1 ? "0" + month : month;
                        day = (day+"").length == 1 ? "0" +day :day;
                        dataJson.updateDateString = year + "-" + month + "-" +day;

                        var arr = dataJson.fileName.split('.');
                        var fileType = '.' + arr[arr.length - 1];
                        dataJson.file_class = iconClassMap.getIconClass(fileType);
                        newTwitterFiles[i] = dataJson;
                    });
                    json.data.twitterFiles = newTwitterFiles;
                }
                json.data.thisUserId = window.SESSION.user.id;
                var html = rk.templateText(temp,json.data);
                elem.html(html);
            };
            me.loaddataParam = {"type":type,param:param};
            if(type == "fileList"){
                me.computePathCrumbs(param.pId,param.pname);
                rk.post("/json/oa_twitter-file/list-group-files.action",param,function(json){
                    if(json.status === 0){
                        me._indexTableAuthMoved = json.data.auth.moved;
                        showHtml(json);
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            }else if(type == "fileSearch"){
                me.pathCrumbs = [];
                me.pathCrumbs[0] = {text:rk.i18n('GROUP_SEARCH_RESULT')};
                rk.post("/json/oa_twitter-file/search-group-files.action",param,function(json){
                    if(json.status === 0){
                        json.data.auth.moved = me._indexTableAuthMoved;
                        showHtml(json);
                        elem.find("[act='group_file_query_name']").val(param.key);
                    }else{
                        rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                    }
                });
            }
        }
    });
});